function auto2()

    % 主窗口
    fig = figure('Name', 'Stackedplot 自动刷新工具', ...
                 'NumberTitle', 'off', ...
                 'MenuBar', 'none', ...
                 'ToolBar', 'none', ...
                 'Units', 'normalized', ...
                 'Position', [0.1, 0.1, 0.8, 0.6]);

    % 分割左右面板
    leftPanel = uipanel(fig, 'Title', '控制面板', ...
                        'FontSize', 12, ...
                        'Units', 'normalized', ...
                        'Position', [0.02, 0.05, 0.3, 0.9]);

    rightPanel = uipanel(fig, 'Title', '绘图区域', ...
                         'FontSize', 12, ...
                         'Units', 'normalized', ...
                         'Position', [0.35, 0.05, 0.63, 0.9]);

    % 文件名输入框
    uicontrol(leftPanel, 'Style', 'text', ...
              'String', 'CSV 文件名:', ...
              'Units', 'normalized', ...
              'Position', [0.05, 0.85, 0.3, 0.05]);

    filenameEdit = uicontrol(leftPanel, 'Style', 'edit', ...
                             'String', 'pid1_out.csv', ...
                             'Units', 'normalized', ...
                             'Position', [0.4, 0.85, 0.5, 0.05]);

    % 刷新周期输入框
    uicontrol(leftPanel, 'Style', 'text', ...
              'String', '刷新周期 (s):', ...
              'Units', 'normalized', ...
              'Position', [0.05, 0.75, 0.3, 0.05]);

    periodEdit = uicontrol(leftPanel, 'Style', 'edit', ...
                           'String', '1.0', ...
                           'Units', 'normalized', ...
                           'Position', [0.4, 0.75, 0.5, 0.05]);

    % 启动/停止按钮
    startStopBtn = uicontrol(leftPanel, 'Style', 'pushbutton', ...
                             'String', '开始刷新', ...
                             'Units', 'normalized', ...
                             'Position', [0.2, 0.6, 0.6, 0.1], ...
                             'BackgroundColor', [0.9, 0.95, 0.9]);

    % 状态显示文本框
    statusText = uicontrol(leftPanel, 'Style', 'text', ...
                           'String', '状态: 等待...', ...
                           'HorizontalAlignment', 'left', ...
                           'Units', 'normalized', ...
                           'Position', [0.05, 0.4, 0.9, 0.15]);

    % 创建绘图区域 figure(1)
    plotFig = figure('Name', 'Stacked Plot Auto Refresh', ...
                     'NumberTitle', 'off', ...
                     'Visible', 'off');
    movegui(plotFig, 'center');  % 居中显示
    figure(plotFig); clf;

    % 初始化定时器句柄为空
    appData.timer = [];
    appData.plotFig = plotFig;
    appData.statusHandle = statusText;
    appData.filenameHandle = filenameEdit;
    appData.periodHandle = periodEdit;
    guidata(fig, appData);  % 存储数据到 figure

    % 按钮回调函数
    set(startStopBtn, 'Callback', @toggleTimer);

    % === 内部函数：切换定时器 ===
    function toggleTimer(~, ~)
        appData = guidata(fig);
        if isempty(appData.timer) || ~isvalid(appData.timer)
            % 启动定时器
            try
                refreshPeriod = str2double(get(appData.periodHandle, 'String'));
                if isnan(refreshPeriod) || refreshPeriod <= 0
                    set(appData.statusHandle, 'String', '错误：请输入正数作为刷新周期');
                    return;
                end

                filename = get(appData.filenameHandle, 'String');

                t = timer('ExecutionMode', 'fixedRate', ...
                          'Period', refreshPeriod, ...
                          'TimerFcn', @(~,~) safePlot(filename, appData.plotFig));

                appData.timer = t;
                start(t);
                set(startStopBtn, 'String', '停止刷新', 'BackgroundColor', [0.95, 0.9, 0.9]);
                set(appData.statusHandle, 'String', '状态：正在刷新...');
                guidata(fig, appData);

            catch ME
                set(appData.statusHandle, 'String', ['错误：' ME.message]);
            end
        else
            % 停止并删除定时器
            stop(appData.timer);
            delete(appData.timer);
            appData.timer = [];
            set(startStopBtn, 'String', '开始刷新', 'BackgroundColor', [0.9, 0.95, 0.9]);
            set(appData.statusHandle, 'String', '状态：已停止');
            guidata(fig, appData);
        end
    end
end

%% 安全绘图函数
function safePlot(filename, plotFig)
    try
        data = readtable(filename, 'ReadVariableNames', true);
        if height(data) == 0
            return;
        end

        figure(plotFig); clf;
        stackedplot(data);
        title(['[更新时间: ' char(datetime('now')) ']']);

    catch ME
        fprintf('❌ 错误: %s\n', ME.message,ME);
    end
end